/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 * Licensed under the Apache License, Version 2.0(the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <pthread.h>
#include "sim_port_adapt.h"

#if defined(OS_FREERTOS)
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#elif defined(OS_NUTTX)
#include <sched.h>
#include <semaphore.h>
#else
#error "No OS defined! Please define OS_LINUX/OS_FREERTOS/OS_NUTTX"
#endif

sim_port_err_t sim_port_sem_init(sim_port_sem_t *sem, uint32_t init_count)
{
    if (NULL == sem) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_FREERTOS)
    SemaphoreHandle_t hsem;

    hsem = xSemaphoreCreateCounting(init_count, init_count);
    if (NULL == hsem) {
        return SIM_PORT_ERROR;
    }
    *sem = hsem;
    return SIM_PORT_SUCCESS;
#elif defined(OS_NUTTX)
    sem_t *psem;

    psem = malloc(sizeof(sem_t));
    if (sem_init(psem, 0, init_count) != 0) {
        free(psem);
        return SIM_PORT_ERROR;
    }
    *sem = psem;
    return SIM_PORT_SUCCESS;
#endif
}

sim_port_err_t sim_port_sem_post(sim_port_sem_t sem)
{
    if (NULL == sem) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_NUTTX)
    return (0 == sem_post((sem_t *)sem)) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;
#elif defined(OS_FREERTOS)
    return (xSemaphoreGive((SemaphoreHandle_t)sem) == pdTRUE) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;
#endif
}

sim_port_err_t sim_port_sem_wait(sim_port_sem_t sem)
{
    if (NULL == sem) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_NUTTX)
    return (0 == sem_wait((sem_t *)sem)) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;
#elif defined(OS_FREERTOS)
    return (xSemaphoreTake((SemaphoreHandle_t)sem, sim_portMAX_DELAY) == pdTRUE) ? SIM_PORT_SUCCESS :
                                               SIM_PORT_ERROR;
#endif
}

sim_port_err_t sim_port_sem_destroy(sim_port_sem_t sem)
{
    if (NULL == sem) {
        return SIM_PORT_ERROR;
    }

#if defined(OS_FREERTOS)
    vSemaphoreDelete((SemaphoreHandle_t)sem);
    return SIM_PORT_SUCCESS;
#elif defined(OS_NUTTX)
    sem_destroy((sem_t *)sem);
    free(sem);
    return SIM_PORT_SUCCESS;
#endif
}

sim_port_err_t sim_port_mutex_init(sim_port_mutex_t *mutex)
{
    if (NULL == mutex) {
        return SIM_PORT_ERROR;
    }

#if defined(OS_NUTTX)
    pthread_mutex_t *nmutex;

    nmutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
    if (NULL == nmutex) {
        return SIM_PORT_ERROR;
    }

    if (0 != pthread_mutex_init(nmutex, NULL)) {
        free(nmutex);
        return SIM_PORT_ERROR;
    }
    *mutex = (sim_port_mutex_t)nmutex;
    return SIM_PORT_SUCCESS;

#elif defined(OS_FREERTOS)
    SemaphoreHandle_t xmutex;

    xmutex = xSemaphoreCreateMutex();
    if (NULL == xmutex) {
        return SIM_PORT_ERROR;
    }
    *mutex = (sim_port_mutex_t)xmutex;
    return SIM_PORT_SUCCESS;
#endif
}

sim_port_err_t sim_port_mutex_lock(sim_port_mutex_t mutex)
{
    if (NULL == mutex) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_NUTTX)
    int ret;

    ret = pthread_mutex_lock((pthread_mutex_t *)mutex);
    return (ret == 0) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;

#elif defined(OS_FREERTOS)
    return (pdTRUE == xSemaphoreTake((SemaphoreHandle_t)mutex, sim_portMAX_DELAY)) ? SIM_PORT_SUCCESS :
                                             SIM_PORT_ERROR;
#endif
}

sim_port_err_t sim_port_mutex_unlock(sim_port_mutex_t mutex)
{
    if (NULL == mutex) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_NUTTX)
    int ret;

    ret = pthread_mutex_unlock((pthread_mutex_t *)mutex);
    return (ret == 0) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;

#elif defined(OS_FREERTOS)
    return (pdTRUE == xSemaphoreGive((SemaphoreHandle_t)mutex)) ? SIM_PORT_SUCCESS : SIM_PORT_ERROR;
#endif
}

sim_port_err_t sim_port_mutex_destroy(sim_port_mutex_t mutex)
{
    if (NULL == mutex) {
        return SIM_PORT_ERROR;
    }
#if defined(OS_NUTTX)
    if (0 != pthread_mutex_destroy((pthread_mutex_t *)mutex)) {
        return SIM_PORT_ERROR;
    }
    free(mutex);
    return SIM_PORT_SUCCESS;

#elif defined(OS_FREERTOS)
    vSemaphoreDelete((SemaphoreHandle_t)mutex);
    return SIM_PORT_SUCCESS;
#endif
}
